package org.orzeszek.transfer;

import javax.swing.JFrame;
import javax.swing.UIManager;

import org.orzeszek.transfer.gui.ApplicationWindow;
import org.orzeszek.transfer.utilities.Constants;

import com.apple.eawt.Application;
import com.apple.eawt.ApplicationAdapter;
import com.apple.eawt.ApplicationEvent;

/**
 * The main application class.
 * 
 * @author Michael Gierasimiuk
 */
public class TransferApplication extends ApplicationAdapter
{
  private Application application;
  private ApplicationWindow window;
  private Settings settings;

  /**
   * Creates a new {@link TransferApplication}.
   */
  public TransferApplication()
  {
    this( ApplicationWindow.DEFAULT_W, ApplicationWindow.DEFAULT_H );
  }

  /**
   * Creates a new {@link TransferApplication} using the given width and height.
   * 
   * @param width the initial pixel width of the application window.
   * @param height the initial pixel height of the application window.
   */
  public TransferApplication( int width, int height )
  {
    System.setProperty( Constants.MENU_NAME_KEY, Constants.MENU_NAME_VAL );

    this.window = new ApplicationWindow();

    this.window.setSize( width, height );
    this.window.centerWindow();
    this.window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    this.settings = new SettingsImpl();
  }

  /**
   * Initializes the {@link TransferApplication} by setting the look and feel,
   * creating the appropriate menu items and setting the frame visible.
   */
  public final void initialize()
  {
    try
    {
      if ( isOsx() )
      {
        this.application = Application.getApplication();
        this.application.addApplicationListener( this );

        this.application.addAboutMenuItem();
        this.application.setEnabledAboutMenu( true );

        this.application.addPreferencesMenuItem();
        this.application.setEnabledPreferencesMenu( true );
      }
      UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
    }
    catch ( Exception e )
    {
      e.printStackTrace();
    }
    this.window.setVisible( true );
  }

  /**
   * Returns true if the {@link TransferApplication} is being executed on a
   * machine running OS X.
   * 
   * @return true if the application is being executed on OS X.
   */
  public boolean isOsx()
  {
    String osName = System.getProperty( "os.name" );
    if ( osName.toLowerCase().contains( "mac" ) )
    {
      return true;
    }
    return false;
  }

  /**
   * Called when the user selects the about option from the file drop down menu.
   * 
   * @param aApplicationEvent the {@link ApplicationEvent}.
   */
  @Override
  public void handleAbout( ApplicationEvent aApplicationEvent )
  {
    
  }

  /**
   * Called when the user selects the preferences option from the file drop down menu.
   * 
   * @param aApplicationEvent the {@link ApplicationEvent}.
   */
  @Override
  public void handlePreferences( ApplicationEvent aApplicationEvent )
  {
    this.window.showSettingsDialog( this.settings );
  }

  /**
   * Called when the user quits the application, just prior to the application closing.
   * 
   * @param aApplicationEvent the {@link ApplicationEvent}.
   */
  @Override
  public void handleQuit( ApplicationEvent aApplicationEvent )
  {
    this.application.removeApplicationListener( this );
  }
}
